package com.qiall.project.service.taskinfo;

import com.qiall.assertutil.Assert;
import com.qiall.common.entity.PageQuery;
import com.qiall.common.entity.PageResponse;
import com.qiall.common.exception.BusinessResult;
import com.qiall.common.request.GetByIdRequest;
import com.qiall.project.ability.request.AddOrUpdateTaskRequest;
import com.qiall.project.ability.request.TaskDTO;
import com.qiall.project.data.entity.ProjectInfo;
import com.qiall.project.data.entity.TaskInfo;
import com.qiall.project.data.enums.TaskStatusEnum;
import com.qiall.project.data.enums.TaskTypeEnum;
import com.qiall.project.data.mapper.ProjectInfoMapper;
import com.qiall.user.ability.entity.GetByAccountRequest;
import com.qiall.user.ability.entity.OrgPageQueryDTO;
import com.qiall.user.ability.entity.OrganizationResponse;
import com.qiall.user.ability.entity.UserInfoDTO;
import com.qiall.user.ability.service.organization.OrganizationService;
import com.qiall.user.ability.service.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴金辉
 */
@Component
public class TaskTrans {

    @DubboReference
    private UserService userService;

    @DubboReference
    private OrganizationService organizationService;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;


    public TaskInfo trans(AddOrUpdateTaskRequest request) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setTaskName(request.getTaskName());
        TaskTypeEnum taskTypeEnum = TaskTypeEnum.of(request.getTaskType());
        taskInfo.setTaskType(taskTypeEnum.getCode());
        TaskStatusEnum taskStatusEnum = TaskStatusEnum.of(request.getTaskStatus());
        taskInfo.setTaskStatus(taskStatusEnum.getCode());
        GetByAccountRequest getByAccountRequest = new GetByAccountRequest();
        getByAccountRequest.setAccount(request.getTaskOwnerAccount());
        getByAccountRequest.setAccessToken(request.getAccessToken());
        BusinessResult<List<UserInfoDTO>> taskOwner = userService.getUserInfoByOrgAccount(getByAccountRequest);
        taskInfo.setTaskOwnerId(taskOwner.getBody().get(0).getUserId());
        taskInfo.setContext(request.getContext());
        //
        if (request.getId() == null) {
            BusinessResult<UserInfoDTO> result = userService.getUserInfoByAuthCode(request);
            Assert.notNull(result.getBody(), "用户未登录");
            taskInfo.setCreatorId(result.getBody().getUserId());
        }
        List<String> taskNumbers = new ArrayList<>();
        if (StringUtils.isNotBlank(request.getTaskMembers())) {
            String[] numbers = request.getTaskMembers().split(",");
            for (String number : numbers) {
                GetByAccountRequest byAccountRequest = new GetByAccountRequest();
                byAccountRequest.setAccessToken(request.getAccessToken());
                byAccountRequest.setAccount(number);
                BusinessResult<List<UserInfoDTO>> numberDTO = userService.getUserInfoByOrgAccount(byAccountRequest);
                taskNumbers.add(numberDTO.getBody().get(0).getUserId() + "");
            }
        }
        String joinNumber = taskNumbers.stream().collect(Collectors.joining(","));
        taskInfo.setTaskMemberIds(joinNumber);
        taskInfo.setAttributes(request.getAttributes());
        // 查询组织
        OrgPageQueryDTO orgPageQueryDTO = new OrgPageQueryDTO();
        orgPageQueryDTO.setOrgName(request.getOrgName());
        orgPageQueryDTO.setPageQuery(PageQuery.getDefault());
        BusinessResult<PageResponse<OrganizationResponse>> orgResponse=
        organizationService.getOrganizationByPage(orgPageQueryDTO);
        taskInfo.setOrgId(orgResponse.getBody().getDataList().get(0).getId());
        ProjectInfo projectInfo=projectInfoMapper.selectByName(request.getProjectName());
        taskInfo.setProjectId(projectInfo.getId());
        taskInfo.setId(request.getId());
        return taskInfo;
    }


    public TaskInfo trans(TaskDTO request) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setTaskName(request.getTaskName());
        if (StringUtils.isNotBlank(request.getTaskType())) {
            TaskTypeEnum taskTypeEnum = TaskTypeEnum.of(request.getTaskType());
            taskInfo.setTaskType(taskTypeEnum.getCode());
        }
        if (StringUtils.isNotBlank(request.getTaskStatus())) {
            TaskStatusEnum taskStatusEnum = TaskStatusEnum.of(request.getTaskStatus());
            taskInfo.setTaskStatus(taskStatusEnum.getCode());
        }
        taskInfo.setTaskOwnerId(request.getTaskOwnerId());
        taskInfo.setContext(request.getContext());
        taskInfo.setCreatorId(request.getCreatorId());
        taskInfo.setTaskMemberIds(request.getTaskMemberIds());
        taskInfo.setAttributes(request.getAttributes());
        taskInfo.setProjectId(request.getProjectId());
        taskInfo.setId(request.getId());
        return taskInfo;
    }

    public TaskDTO trans(TaskInfo taskInfo) {
        TaskDTO taskDTO = new TaskDTO();
        taskDTO.setId(taskInfo.getId());
        taskDTO.setTaskName(taskInfo.getTaskName());
        taskDTO.setTaskType(TaskTypeEnum.of(taskInfo.getTaskType()).getDesc());
        TaskStatusEnum taskStatusEnum = TaskStatusEnum.of(taskInfo.getTaskStatus());
        taskDTO.setTaskStatus(taskStatusEnum.getDesc());
        BusinessResult<UserInfoDTO> ownerInfoDTO = userService.getUserInfo(taskInfo.getTaskOwnerId());
        taskDTO.setTaskOwnerAccount(ownerInfoDTO.getBody().getUserName());
        taskDTO.setContext(taskInfo.getContext());
        taskDTO.setCreatorId(taskInfo.getCreatorId());
        BusinessResult<UserInfoDTO> creatorDTO = userService.getUserInfo(taskInfo.getCreatorId());
        taskDTO.setCreatorName(creatorDTO.getBody().getUserName());
        taskDTO.setTaskMemberIds(taskInfo.getTaskMemberIds());
        List<String> names = new ArrayList<>();
        if (StringUtils.isNotBlank(taskInfo.getTaskMemberIds())) {
            String[] userIds = taskInfo.getTaskMemberIds().split(",");
            for (String userIdString : userIds) {
                int userId = Integer.parseInt(userIdString);
                BusinessResult<UserInfoDTO> tmp = userService.getUserInfo(userId);
                names.add(tmp.getBody().getUserName());
            }
            taskDTO.setTaskMembers(names.stream().collect(Collectors.joining(",")));
        }
        taskDTO.setAttributes(taskInfo.getAttributes());
        taskDTO.setOrgId(taskInfo.getOrgId());
        GetByIdRequest getByIdRequest = new GetByIdRequest();
        getByIdRequest.setId(taskInfo.getOrgId());
        BusinessResult<OrganizationResponse> organizationResponseBusinessResult = organizationService.getOrganizationInfo(getByIdRequest);
        taskDTO.setOrgName(organizationResponseBusinessResult.getBody().getOrgName());
        taskDTO.setProjectId(taskInfo.getProjectId());
        ProjectInfo projectInfo = projectInfoMapper.select(taskInfo.getProjectId());
        taskDTO.setProjectName(projectInfo.getProjectName());
        taskDTO.setCreateTime(taskInfo.getCreateTime());
        return taskDTO;
    }
}
